<?php
namespace app\common\server;
use x\Xbase;
class OrderUmoney extends Xbase{

    /**
     * 系统增减会员资金
     * @param unknown $user_id
     * @param unknown $action_user_id
     * @param unknown $money_type   资金类型
     * @param unknown $money
     * @param unknown $note
     */
    public function doit($user_id, $action_user_id, $money_type, $money, $note){
        $user = $this->db->find('user',$user_id);

        $order_no = $this->com_order_no();
        $this->db->begin();
        $mysql   = [];
        #订单
        $d = [
            'user_id'       => $user_id,
            'order_no'      => $order_no,
            'action_user_id'=> $action_user_id,
            'money_type'    => $money_type,
            'money'         => $money,
            'note'          => $note,
            'create_time'   => time(),
        ];
        $mysql[] = $id = $this->db->insert('order_umoney',$d);
        #扣款
        if($money<0){
            $type = 201;
            $real_money = bcsub(0,$money,2);
            $msg = "为用户[{$user['username']}]减少资金{$real_money}";
        }else{
            $type = 101;
            $real_money = bcadd(0,$money,2);
            $msg = "为用户[{$user['username']}]增加资金{$real_money}";
        }
        $mysql[] = m('Userbill','xcom')->doit($order_no, $user_id, $type, 'order_umoney', $id,  $real_money,$money_type,$note);
        $rs = $this->db->end($mysql);
        if(!$rs){
            $this->setError('变动资金失败');
            return false;
        }else{
            m('Log','xcom')->record($action_user_id, $msg,'admin_user');
            return true;
        }
    }
}
